package com.lzh.persevere.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.lzh.persevere.config.constant.RedisConstants;
import com.lzh.persevere.mapper.FileInfoMapper;
import com.lzh.persevere.mapper.UserMapper;
import com.lzh.persevere.model.entity.User;
import com.lzh.persevere.utils.RedisCache;
import com.lzh.persevere.utils.VerificationCodeGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author luzhiheng
 * @since 2025-07-08
 */
@Service
public class RedisCacheService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisCache redisCache;

    @Resource
    private FileInfoMapper fileInfoMapper;

    /**
     * 获取所有用户id和昵称 Map
     *
     * @return Map<Long, String>
     */
    public Map<String, String> getAllUserIdNickNameMap() {
        String key = "lzh:user:id_nickName_map";
        Map<String, Object> cacheMap = redisCache.getCacheMap(key);
        if (cacheMap != null && !cacheMap.isEmpty()) {
            Map<String, String> resultMap = new HashMap<>();
            cacheMap.forEach((k, v) -> resultMap.put(k, v.toString()));
            return resultMap;
        } else {
            List<User> userList = userMapper.selectList(null);
            Map<String, String> userIdNickNameMap = userList.stream().collect(Collectors.toMap(e -> String.valueOf(e.getId()), User::getNickName));
            redisCache.setCacheMap(key, userIdNickNameMap);
            redisCache.expire(key, 5, TimeUnit.DAYS);
            return userIdNickNameMap;
        }
    }

    /**
     * 获取所有用户id和头像 Map
     *
     * @return Map<Long, String>
     */
    public Map<String, String> getAllUserIdAvatarMap() {
        String key = RedisConstants.USER_ID_AVATAR_MAP;
        Map<String, Object> cacheMap = redisCache.getCacheMap(key);
        if (cacheMap != null && !cacheMap.isEmpty()) {
            Map<String, String> resultMap = new HashMap<>();
            cacheMap.forEach((k, v) -> resultMap.put(k, v.toString()));
            return resultMap;
        } else {
            List<User> userList = userMapper.selectList(null).stream().filter(e -> StrUtil.isNotEmpty(e.getAvatar())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(userList)) {
                Map<String, String> userIdAvatarMap = userList.stream().collect(Collectors.toMap(e -> String.valueOf(e.getId()), User::getAvatar));
                redisCache.setCacheMap(key, userIdAvatarMap);
                redisCache.expire(key, 5, TimeUnit.DAYS);
                return userIdAvatarMap;
            } else {
                return new HashMap<>();
            }
        }
    }

    /**
     * 生成注册验证码
     *
     * @param email 邮箱
     */
    public String generateRegisterCode(String email) {
        String code = VerificationCodeGenerator.generateNumericCode(6);
        redisCache.setCacheObject("registerCode:" + email, code, 15, TimeUnit.MINUTES);
        return code;
    }

    /**
     * 获取注册验证码
     *
     * @param email    邮箱
     */
    public String getRegisterCode(String email) {
        return redisCache.getCacheObject("registerCode:" + email);
    }
}
